package com.ruoyi.cbs.approval.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.cbs.approval.domain.vo.AddApprovalConfigReqVO;
import com.ruoyi.cbs.approval.domain.vo.ApprovalConfigRespVO;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.cbs.approval.mapper.ApprovalConfigMapper;
import com.ruoyi.cbs.approval.domain.ApprovalConfig;
import com.ruoyi.cbs.approval.service.IApprovalConfigService;

/**
 * 审核配置Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-11-12
 */
@Service
public class ApprovalConfigServiceImpl implements IApprovalConfigService 
{
    @Autowired
    private ApprovalConfigMapper approvalConfigMapper;

    /**
     * 查询审核配置
     * 
     * @param id 审核配置主键
     * @return 审核配置
     */
    @Override
    public ApprovalConfig selectApprovalConfigById(Long id)
    {
        return approvalConfigMapper.selectApprovalConfigById(id);
    }

    /**
     * 查询审核配置列表
     * 
     * @param approvalConfig 审核配置
     * @return 审核配置
     */
    @Override
    public List<ApprovalConfigRespVO> selectApprovalConfigList(ApprovalConfig approvalConfig)
    {
        List<ApprovalConfigRespVO> result = new ArrayList<>();
        List<ApprovalConfig> list = approvalConfigMapper.selectApprovalConfigList(approvalConfig);
        for (ApprovalConfig approval: list) {
            ApprovalConfigRespVO approvalConfigRespVO = new ApprovalConfigRespVO();
            BeanUtils.copyProperties(approval,approvalConfigRespVO);
            result.add(approvalConfigRespVO);
        }
        return result;
    }

    /**
     * 根据审批类型和seqNum查询所有审批节点
     * @param approvalType
     * @param seqNum
     * @return
     */
    public List<ApprovalConfig> selectByTypeAndSeqNum(String approvalType, Integer seqNum) {
        return approvalConfigMapper.selectByTypeAndSeqNum(approvalType, seqNum);
    }

    /**
     * 根据审批类型和seqNum查询下一个审批节点
     * @param approvalType
     * @param seqNum
     * @return
     */
    public List<ApprovalConfig> selectNextByTypeAndSeqNum(String approvalType, Integer seqNum) {
        return approvalConfigMapper.selectNextByTypeAndSeqNum(approvalType, seqNum);
    }

    /**
     * 新增审核配置
     * 
     * @param approvalConfig 审核配置
     * @return 结果
     */
    @Override
    public int insertApprovalConfig(AddApprovalConfigReqVO addApprovalConfigReqVO)
    {
        ApprovalConfig approvalConfig = new ApprovalConfig();
        BeanUtils.copyProperties(addApprovalConfigReqVO,approvalConfig);
        approvalConfig.setCreateTime(DateUtils.getNowDate());
        approvalConfig.setDelFlag("0");
        return approvalConfigMapper.insertApprovalConfig(approvalConfig);
    }

    /**
     * 修改审核配置
     * 
     * @param approvalConfig 审核配置
     * @return 结果
     */
    @Override
    public int updateApprovalConfig(ApprovalConfig approvalConfig)
    {
        approvalConfig.setUpdateTime(DateUtils.getNowDate());
        return approvalConfigMapper.updateApprovalConfig(approvalConfig);
    }

    /**
     * 批量删除审核配置
     * 
     * @param ids 需要删除的审核配置主键
     * @return 结果
     */
    @Override
    public int deleteApprovalConfigByIds(Long[] ids)
    {
        return approvalConfigMapper.deleteApprovalConfigByIds(ids);
    }

    /**
     * 删除审核配置信息
     * 
     * @param id 审核配置主键
     * @return 结果
     */
    @Override
    public int deleteApprovalConfigById(Long id)
    {
        return approvalConfigMapper.deleteApprovalConfigById(id);
    }
}
